package com.xsystem.user.delegates;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xsystem.orm.user.User;
import com.xsystem.user.exception.UserNotFoundException;
import com.xsystem.user.exception.UserUnauthorizedException;

public class UserDelegate extends UserEngineDelegate {

	private static final Logger log = LoggerFactory
			.getLogger(UserDelegate.class);

	// ================================================================================
	// Public Methods
	// ================================================================================

	public User getAuthenticatedUser(String username, String hashedPassword)
			throws UserNotFoundException, UserUnauthorizedException {

		User user = userDAO.findUserByUsername(username);
		
		if (user == null) {
			throw new UserNotFoundException();
		}

		if (!user.getPassword().equals(hashedPassword)) {
			log.error("password not matched");
			throw new UserUnauthorizedException();
		}

		return user;
	}

	public User createUser(String username, String password) {

		User user = new User();

		user.setUsername(username);
		user.setPassword(password);

		userDAO.create(user);
		return user;
	}
}
